#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>
using namespace std;

struct Node
{
	int val;
	Node* left;
	Node* right;
};

bool isBalanced(Node* root,int& depth)
{
	if(!root)
	{
		depth = 0;
		return true;
	}
	int ldepth,rdepth;
	bool childBalanced = isBalanced(root->left,ldepth) & isBalanced(root->right,rdepth);
	depth = max(ldepth,rdepth)+1;
	return childBalanced && (abs(ldepth-rdepth) <= 1) ;
}